import Vue from "vue"
import VueRouter from "vue-router"
import { Message } from "element-ui"

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    name: "Login",
    redirect: "/login"
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/Login.vue")
  },
  {
    path: "/index",
    name: "Index",
    component: () => import("@/layout/index.vue"),
    meta: { requireAuth: true },
    children: [
      {
        path: "/home",
        name: "Home",
        component: () => import("@/views/Home.vue")
      },
      {
        path: "/staffmanage",
        name: "StaffManage",
        component: () => import("@/views/personManage/StaffManage.vue"),
        meta: { requireAuth: true }
      },
      {
        path: "/stumanage",
        name: "StuManage",
        component: () => import("@/views/personManage/StuManage.vue")
      },
      {
        path: "/videowall",
        name: "VideoWall",
        component: () => import("@/views/videoManage/VideoWall.vue")
      },
      {
        path: "/videoreplay",
        name: "VideoReplay",
        component: () => import("@/views/videoManage/VideoReplay.vue")
      },
      {
        path: "/channelconfig",
        name: "ChannelConfig",
        component: () => import("@/views/videoManage/ChannelConfig.vue")
      },
      {
        path: "/warnlist",
        name: "WarnList",
        component: () => import("@/views/warnList/WarnList.vue")
      },
      {
        path: "/Comeout",
        name: "Comeout",
        component: () => import("@/views/outinmanage/Comeout.vue")
      },
      {
        path: "/Carout",
        name: "Carout",
        component: () => import("@/views/outinmanage/Carout.vue")
      },
      {
        path: "/Carwhite",
        name: "Carwhite",
        component: () => import("@/views/outinmanage/Carwhite.vue")
      },
      {
        path: "/Equipment",
        name: "Equipment",
        component: () => import("@/views/equipmentmanage/Equipment.vue")
      },
      {
        path: "/Equipmentclass",
        name: "Equipmentclass",
        component: () => import("@/views/equipmentmanage/Equipmentclass.vue")
      },
      {
        path: "/EmergencyPlan",
        name: "EmergencyPlan",
        component: () =>
          import("@/views/school/emergencyManagement/emergencyPlan.vue")
      },
      {
        path: "/HiddenDangerList",
        name: "HiddenDangerList",
        component: () =>
          import("@/views/school/securityManagement/hiddenDangerList.vue")
      },
      {
        path: "/PatrolPlan",
        name: "PatrolPlan",
        component: () =>
          import("@/views/school/securityManagement/patrolPlan.vue")
      },
      {
        path: "/OrganizationManagement",
        name: "OrganizationManagement",
        component: () =>
          import("@/views/systemSetting/organizationManagement.vue")
      },
      {
        path: "/RoleManagement",
        name: "RoleManagement",
        component: () => import("@/views/systemSetting/roleManagement.vue")
      },
      {
        path: "/UserManagement",
        name: "UserManagement",
        component: () => import("@/views/systemSetting/userManagement.vue")
      }
    ]
  }
]
const router = new VueRouter({
  routes
})

//访问之前检查是否登录了
// router.beforeEach((to,from,next) =>{
//   if (to.path.startsWith('/login')) {
//     window.sessionStorage.removeItem('user');
//     next()
//   }else{
//     let token = window.sessionStorage.getItem('user');
//     if (!token) {
//       next({path: '/login'})
//     }else {
//       next()
//     }
//   }
// })

/***
 * 拦截路由跳转，校验用户的路由权限或做流量控制
 */

//访问之前检查是否登录了

router.beforeEach((to, from, next) => {
  if (to.path.startsWith("/login")) {
    window.sessionStorage.removeItem("token")
    next()
  } else {
    let token = window.sessionStorage.getItem("token")
    token = JSON.parse(token)
    if (!token) {
      next({ path: "/login" })
    } else {
      if (token.sysRouters.includes(to.path)) {
        //判断路由是不是一个资源,登录后需要权限
        if (token.authRouters.includes(to.path)) {
          //在资源路由中，有此路由权限的登录用户才能访问
          next()
        } else {
          Message.warning("没有权限")
        }
      } else {
        next() //登录用户都能访问的路由资源
      }
      // next()   //所有用户都能够访问的路由资源
    }
  }
})
export default router
